/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Progresywny Maciek
 */
public class Utils {

    final private static int RED = 0x00FF0000;
    final private static int WHITE = 0x00FFFFFF;
    final private static int BLACK = 0x00000000;

    public static int ftpsToMilisecond(float value) {
        return Math.round((1.0f / value) * 1000.0f);
    }

    public static int milisecondToFtps(long value) {
        return Math.round(1000 / (int) value);
    }

    public static BufferedImage processingToBlackWhite(Rectangle[] rect, int width, int height) {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
        for (Rectangle r : rect) {
            Graphics2D graph = image.createGraphics();
            graph.setColor(Color.white);
            graph.fill(new Rectangle((int) r.getX(), (int) r.getY(), (int) r.getWidth(), (int) r.getHeight()));
            graph.dispose();
        }
        return image;
    }

    public static BufferedImage getImageAndImage(BufferedImage oneImage, BufferedImage twoImage) {
        BufferedImage out = new BufferedImage(oneImage.getWidth(), oneImage.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
        for (int i = 0; i < twoImage.getHeight(); i++) {
            for (int j = 0; j < oneImage.getWidth(); j++) {
                if (oneImage.getRGB(j, i) == -16777216 && twoImage.getRGB(j, i) == -16777216) {
                    out.setRGB(j, i, BLACK);
                } else {
                    out.setRGB(j, i, WHITE);
                }
            }
        }
        return out;
    }

    public static BufferedImage getSkinToBlackWhite(BufferedImage Image) {
        BufferedImage out = new BufferedImage(Image.getWidth(), Image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);

        for (int i = 0; i < Image.getHeight(); i++) {
            for (int j = 0; j < Image.getWidth(); j++) {
                if (Image.getRGB(j, i) == -65536) {
                    out.setRGB(j, i, BLACK);
                } else {
                    out.setRGB(j, i, WHITE);
                }
            }
        }
        return out;
    }
    
    public static BufferedImage getGrayToBlackWhite(BufferedImage Image) {
        BufferedImage out = new BufferedImage(Image.getWidth(), Image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);

        for (int i = 0; i < Image.getHeight(); i++) {
            for (int j = 0; j < Image.getWidth(); j++) {
                if ((byte)Image.getRGB(j, i) >= 255) {
                    out.setRGB(j, i, WHITE);
                } else {
                    out.setRGB(j, i, BLACK);
                }
            }
        }
        return out;
    }

    public static BufferedImage getMedianSkinToBlackWhite(BufferedImage Image) {
        BufferedImage out = new BufferedImage(Image.getWidth(), Image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);

        for (int i = 0; i < Image.getHeight(); i++) {
            for (int j = 0; j < Image.getWidth(); j++) {
                if (Image.getRGB(j, i) != -1) {
                    out.setRGB(j, i, WHITE);
                } else {
                    out.setRGB(j, i, BLACK);
                }
            }
        }
        return out;
    }

    public static BufferedImage deepCopy(BufferedImage bi) {
        if (bi != null) {
            ColorModel cm = bi.getColorModel();
            boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
            WritableRaster raster = bi.copyData(null);
            return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
        } else {
            return null;
        }
    }
}
